/**
* 教材管理 新书入库管理
*/
<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right" class="bar-font">
			<el-breadcrumb-item>教材管理</el-breadcrumb-item>
			<el-breadcrumb-item>新书入库管理</el-breadcrumb-item>
		</el-breadcrumb>

		<!-- 搜索筛选 -->
		<el-form :inline="true" :model="formInline" class="user-search">
			<el-form-item label="搜索：">
				<el-input size="small" v-model="formInline.bookName" placeholder="输入教材名称"></el-input>
			</el-form-item>
			<el-form-item label="">
				<el-input size="small" v-model="formInline.bookCourseName" placeholder="输入教材所属课程名称"></el-input>
			</el-form-item>
			<el-form-item label="">
				<el-input size="small" v-model="formInline.bookAuthor" placeholder="输入教材作者"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
				<el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
			</el-form-item>
		</el-form>

		<!--列表-->
		<el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
			element-loading-text="拼命加载中" style="width: 100%;">
			<el-table-column align="center" type="selection" width="60">
			</el-table-column>
			<el-table-column sortable prop="bookId" label="教材ID" width="100" v-if="false"></el-table-column>
			<el-table-column sortable prop="bookName" label="教材名称" width="100"></el-table-column>
			<el-table-column sortable prop="bookCourseCode" label="教材所属课程代码" width="150"></el-table-column>
			<el-table-column sortable prop="bookCourseName" label="教材所属课程名称" width="150"></el-table-column>
			<el-table-column sortable prop="bookAuthor" label="教材作者" width="100"></el-table-column>
			<el-table-column sortable prop="bookVersion" label="教材版本" width="100"></el-table-column>
			<el-table-column sortable prop="bookPrice" label="教材价格" width="100"></el-table-column>
			<el-table-column sortable prop="bookIsbn" label="教材ISBN" width="100"></el-table-column>
			<el-table-column sortable prop="bookStatus" label="教材状态" width="100"></el-table-column>
			<el-table-column align="center" label="操作" min-width="200">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页组件 -->
		<!-- <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination> -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
			:page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
			:total="total">
		</el-pagination>
		<!-- 编辑界面 -->
		<el-dialog :title="title" :visible.sync="editFormVisible" width="35%" @click="closeDialog">
			<el-form label-width="150px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="教材名称" prop="bookName">
					<el-input size="small" v-model="editForm.bookName" auto-complete="off" placeholder="请输入教材名称"></el-input>
				</el-form-item>
				<el-form-item label="教材所属课程代码" prop="bookCourseCode">
					<el-input size="small" v-model="editForm.bookCourseCode" auto-complete="off" placeholder="请输入教材所属课程代码"></el-input>
				</el-form-item>
				<el-form-item label="教材所属课程名称" prop="bookCourseName">
					<el-input size="small" v-model="editForm.bookCourseName" auto-complete="off" placeholder="请输入教材所属课程名称"></el-input>
				</el-form-item>
				<el-form-item label="教材作者" prop="bookAuthor">
					<el-input size="small" v-model="editForm.bookAuthor" auto-complete="off" placeholder="请输入教材作者"></el-input>
				</el-form-item>
				<el-form-item label="教材版本" prop="bookVersion">
					<el-input size="small" v-model="editForm.bookVersion" auto-complete="off" placeholder="请输入教材版本"></el-input>
				</el-form-item>
				<el-form-item label="教材价格" prop="bookPrice">
					<el-input size="small" v-model="editForm.bookPrice" auto-complete="off" placeholder="请输入教材价格"></el-input>
				</el-form-item>
				<el-form-item label="教材ISBN" prop="bookIsbn">
					<el-input size="small" v-model="editForm.bookIsbn" auto-complete="off" placeholder="请输入教材ISBN"></el-input>
				</el-form-item>
				<el-form-item label="教材状态" prop="bookStatus">
					<el-input size="small" v-model="editForm.bookStatus" auto-complete="off" placeholder="请输入教材状态"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">
					保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		booksearch,
		bookInsert,
		bookEdit,
		bookDelete
	} from '../../api/getData'
	export default {
		data() {
			return {
				nshow: true, //switch开启
				fshow: false, //switch关闭
				loading: false, //是显示加载
				editFormVisible: false, //控制编辑页面显示与隐藏
				title: '添加',
				editForm: {
					bookName:'',
					bookCourseCode:'',
					bookCourseName:'',
					bookAuthor:'',
					bookVersion:'',
					bookPrice:'',
					bookIsbn:'',
					bookStatus:'',
					token: localStorage.getItem('logintoken')
				},
				// rules表单验证
				rules: {
					bookName: [{
						required: true,
						message: '请输入教材名称',
						trigger: 'blur'
					}],
					bookCourseCode: [{
						required: true,
						message: '请输入教材所属课程代码',
						trigger: 'blur'
					}]
				},
				formInline: {
					bookName:'',
					bookCourseName: '',
					bookAuthor: '',
				},
				// 删除部门
				seletedata: {
					ids: '',
					token: localStorage.getItem('logintoken')
				},
				userparm: [], //搜索权限
				listData: [], //用户数据
				// 分页参数
				pageSizes: [10, 20, 40, 400],
				total: 100,
				currentPage: 1,
				pageSize: 10
			}
		},

		/**
		 * 创建完毕
		 */
		created() {
			this.getdata(this.formInline)
		},

		/**
		 * 里面的方法只有被调用才会执行
		 */
		methods: {
			// 获取公司列表
			getdata(parameter) {
				this.formInline.page = this.currentPage;
				this.formInline.limit = this.pageSize
				this.loading = true


				booksearch(parameter)
					.then(res => {
						this.loading = false
						console.log(res)
						if (res != -1) {
							this.listData = res.datas
							// 分页赋值
							this.total = res.total
						}
					})
					.catch(err => {
						this.loading = false
						this.$message.error('菜单加载失败，请稍后再试！')
					})
			},
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.pageSize = val;
				this.search();
			},
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.currentPage = val;
				this.search();
			},
			// 分页插件事件
			callFather(parm) {
				this.formInline.page = parm.currentPage
				this.formInline.limit = parm.pageSize
				this.getdata(this.formInline)
			},
			// 搜索事件
			search() {
				this.getdata(this.formInline)
			},
			//显示编辑界面
			handleEdit: function(index, row) {
				this.editFormVisible = true
				if (row != undefined && row != 'undefined') {
					console.log(row.examId)
					this.title = '修改'
					this.editForm.bookId = row.bookId
					this.editForm.bookName = row.bookName
					this.editForm.bookCourseCode = row.bookCourseCode
					this.editForm.bookCourseName = row.bookCourseName
					this.editForm.bookAuthor = row.bookAuthor
					this.editForm.bookVersion = row.bookVersion
					this.editForm.bookPrice = row.bookPrice
					this.editForm.bookIsbn = row.bookIsbn
					this.editForm.bookStatus = row.bookStatus
				} else {
					this.title = '添加'
					this.editForm.bookName = ''
					this.editForm.bookCourseCode = ''
					this.editForm.bookCourseName = ''
					this.editForm.bookAuthor = ''
					this.editForm.bookVersion = ''
					this.editForm.bookPrice = ''
					this.editForm.bookIsbn = ''
					this.editForm.bookStatus = ''

				}
			},
			// 编辑、增加页面保存方法
			submitForm(editData) {
				this.$refs[editData].validate(valid => {
					if (valid) {
						if (this.title == '添加') {
							console.log("进入添加模块")
							bookInsert(this.editForm).then(res => {
								console.log(res)
								this.editFormVisible = false
								this.loading = false

								if (res != -1) {
									this.getdata(this.formInline)
									this.$message({
										type: 'success',
										message: '教材添加成功!'
									})
								} else {
									this.$message({
										type: 'error',
										message: '教材添加失败!'
									})
								}
							})
						} else if (this.title == '修改') {
							console.log(this.editForm)
							console.log("进入修改模块")
							bookEdit(this.editForm).then(res => {
								console.log(res)
								this.editFormVisible = false
								this.loading = false
								if (res != -1) {
									this.getdata(this.formInline)
									this.$message({
										type: 'success',
										message: '教材修改成功!'
									})
								} else {
									this.$message({
										type: 'error',
										message: '教材修改失败!'
									})
								}
							})
						}
					} else {
						return false
					}
				})
			},
			// 删除公司
			deleteUser(index, row) {
				this.$confirm('确定要删除吗?', '信息', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						bookDelete({"bookId":row.bookId})
							.then(res => {
								console.log(res)
								if (res != -1) {
									this.$message({
										type: 'success',
										message: '教材删除成功!'
									})
									this.getdata(this.formInline)
								} else {
									this.$message({
										type: 'error',
										message: '教材删除失败!'
									})
								}
							})
							.catch(err => {
								this.loading = false
								this.$message.error('教材删除失败，请稍后再试！')
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 关闭编辑、增加弹出框
			closeDialog() {
				this.editFormVisible = false
			}
		}
	}
</script>

<style scoped>
	.user-search {
		margin-top: 20px;
	}

	.userRole {
		width: 100%;
	}
	.bar-font {
		font-weight: 900;
	}
</style>
